package com.jet.merchant.service.impl;

import cn.hutool.json.JSONUtil;
import com.jet.core.utils.IdUtil;
import com.jet.merchant.domain.entity.ClerkAccountScan;
import com.jet.merchant.domain.enums.AccountScanStatus;
import com.jet.merchant.domain.vo.AccountScanStatusVo;
import com.jet.merchant.domain.bo.LoginBo;
import com.jet.merchant.mapper.ClerkAccountScanMapper;
import com.jet.merchant.service.IClerkAccountScanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneOffset;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-02-25
 */
@Service
public class ClerkAccountScanServiceImpl extends ServiceImpl<ClerkAccountScanMapper, ClerkAccountScan> implements IClerkAccountScanService {

    @Override
    public String save(int source) {
        ClerkAccountScan entity = new ClerkAccountScan();
        entity.setId(IdUtil.generateLower());
        entity.setSource(source);
        entity.setStatus(AccountScanStatus.CREATED.getCode());
        entity.setExpireTime(getExpireTime());
        super.save(entity);
        return entity.getId();
    }

    @Override
    public void editStatus(String id, int status) {
        ClerkAccountScan entity = new ClerkAccountScan();
        entity.setId(id);
        entity.setStatus(status);
        super.updateById(entity);
    }

    @Override
    public void editLoginInfo(String id, LoginBo loginInfo) {
        ClerkAccountScan entity = new ClerkAccountScan();
        entity.setId(id);
        entity.setLoginInfo(loginInfo);
        entity.setStatus(AccountScanStatus.LOGIN.getCode());
        super.updateById(entity);
    }

    @Override
    public AccountScanStatusVo findStatus(String id) {
        ClerkAccountScan entity = super.getById(id);
        AccountScanStatusVo vo = validScanStatus(entity);
        if (vo != null) {
            return vo;
        }
        vo = new AccountScanStatusVo();
        vo.setStatus(entity.getStatus());
        vo.setLoginVo(entity.getLoginInfo());
        return vo;
    }

    /**
     * 验证是否过期和取消
     *
     * @param entity
     * @return
     */
    @Override
    public AccountScanStatusVo validScanStatus(ClerkAccountScan entity) {
        AccountScanStatusVo vo = new AccountScanStatusVo();
        long timestamp = getTimestamp();
        if (entity == null || entity.getStatus() == AccountScanStatus.EXPIRED.getCode()) {
            vo.setStatus(AccountScanStatus.EXPIRED.getCode());
            return vo;
        }
        if (entity.getStatus() == AccountScanStatus.CANCEL.getCode()) {
            vo.setStatus(AccountScanStatus.CANCEL.getCode());
            return vo;
        }
        if (entity.getExpireTime() < timestamp) {
            editStatus(entity.getId(), AccountScanStatus.EXPIRED.getCode());
            vo.setStatus(AccountScanStatus.EXPIRED.getCode());
            return vo;
        }
        return null;
    }

    private long getTimestamp() {
        return LocalDateTime.now().toInstant(ZoneOffset.UTC).toEpochMilli();
    }

    private long getExpireTime() {
        LocalDateTime now = LocalDateTime.now().plusMinutes(2);
        return now.toInstant(ZoneOffset.UTC).toEpochMilli();
    }
}
